package com.sgcc.fj.pmos.e_fj_home.activity;

import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.fj.pmos.arouter.home.HomeArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_home.adapter.UserTypeHisAdapter;
import com.sgcc.fj.pmos.e_fj_home.bean.DynamicBean;
import com.sgcc.fj.pmos.e_fj_home.bean.UserTypeHistoryBean;
import com.sgcc.fj.pmos.e_fj_home.contract.UserHistoryContract;
import com.sgcc.fj.pmos.e_fj_home.presenter.UserHistoryPresenter;

import java.util.Calendar;
import java.util.Date;

/**
 * 用户类型切换历史记录
 */
@Route(path = HomeArouterPath.HOME_USERTYPEHIS)
public class UsertTypeHistoryActivity extends MvpBaseRecycleActivity<UserTypeHistoryBean.DataDTO.ListDTO, UserHistoryPresenter> implements UserHistoryContract.View {

    private UserTypeHisAdapter userTypeHisAdapter;
    private Switch hide_switch;
    private LinearLayout linner_time;
    private TextView tv_time;
    int year  = Calendar.getInstance().get(Calendar.YEAR);
    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    TimePickerView pvTime;
    private String selecttime;
    private boolean isShowRecord = false;

    @Override
    protected UserHistoryPresenter createPresenter() {
        return new UserHistoryPresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        userTypeHisAdapter = new UserTypeHisAdapter(mContext);
        userTypeHisAdapter.setOnItemClickListener(new UserTypeHisAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, DynamicBean bean) {
//                ARouter.getInstance()
//                        .build(HomeArouterPath.HOME_PUBLICLAWSDYNAMICINFO)
//                        .withSerializable("mDynamicBean", bean)
//                        .withString("from", "2")
//                        .navigation();
            }
        });
        return userTypeHisAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);

        mPresenter.getHistoryList(year + "", isShowRecord);
    }

//    @Override
//    public String getTitleText() {
//        return "身份转换记录";
//    }

    @Override
    protected void initViews() {
        super.initViews();
//        title_bar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                goMainActivity();
//            }
//        });
        title_bar.setTitle("身份转换记录");
        title_bar.setRightText(year + "年", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar1=    Calendar.getInstance();
                calendar1.set(Calendar.YEAR,year);
                pvTime.setDate(calendar1);
                pvTime.show();
            }
        });
        title_bar.setRightIcon(R.mipmap.down_jiantou, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar1=    Calendar.getInstance();
                calendar1.set(Calendar.YEAR,year);
                pvTime.setDate(calendar1);
                pvTime.show();
//                goMainActivity();
            }
        });
        initTimeSelect();
    }

    @Override
    public void querySuccess(UserTypeHistoryBean respon) {
        if (respon != null) {
            if( respon.getData()!=null){
                if( respon.getData().getList()!=null){
                    loadSuccess(false, respon.getData().getList());
                }
            }
        } else {
            ToastUtils.showShort("返回数据有误");
        }
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_usethis, parent, false);
        findViewByIds(head_view);
        return head_view;
    }

    private void findViewByIds(View view) {
        hide_switch = view.findViewById(R.id.hide_switch);
        tv_time = view.findViewById(R.id.tv_time);
        hide_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isShowRecord = b;
                mPresenter.getHistoryList(year + "", isShowRecord);
            }
        });
    }

    @Override
    public void queryFaile() {

    }

    public void setTime(String time) {
        year = Integer.valueOf(time);
        refresh(false);
    }

    private void initTimeSelect() {
        //正确设置方式 原因：注意事项有说明
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        // 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        startDate.set(2021, month, 1);
        endDate.set(2099, 11, 1);
        //时间选择器
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = TimeUtils.date2String(date, "yyyy");
                title_bar.tvRight.setText(time + "年");
                setTime(time);
//                mPresenter.getHistoryList(selecttime, isShowRecord);
            }
        }).setType(new boolean[]{true, false, false, false, false, false})
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content))
                .setRangDate(startDate, endDate)
                .build();
    }
}
